package edu.ruc.cluster.print;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
import java.util.Date;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;

@SuppressWarnings("serial")
public class TestConsolePane extends JFrame {

	public TestConsolePane(){
		setTitle("Redirect System.out");
		setSize(640, 240);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		getContentPane().add(ConsolePane.getInstance());
		JButton button = new JButton("Click Me to Output Message");
		getContentPane().add(button, BorderLayout.SOUTH);
		
		button.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				Random random = new Random();
				int num = random.nextInt(10);
				String msg = ": Hello Yatou, Redirect"
						+ ((num %2 == 1) ? "\"System.out\"" : "\"System.err\"")
						+ " to ConsolePane, today:";
				if(num % 2 == 1)
					System.out.println(num + msg + new Date());
				else
					System.err.println(num + msg + new Date());
			}
		});
		setVisible(true);
	}
	public static void main(String[] args){
		new TestConsolePane();
	}
}
